<?php

namespace app\order\logic;

use app\common\library\RedissLock;
use app\common\service\JinyiApiService;
use app\common\service\YiJTApiService;
use app\common\service\ShunfengApiService;
use think\facade\Db;
use think\facade\Cache;

class OrderShipServiceLogic
{
    /**
     * 订单发货队列消费
     * @param $data
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderShippingQueueConsume($data)
    {

        htmlLog(['data'=>$data],OPERATE_LOG . date('Ym') . '/orderShippingQueueConsume/' . date('Ymd') . '.txt');

      /*  $redis_lock = new RedissLock();
        $key        = 'orderShippingQueueConsume:'.$data['order_id'];
        $lock       = $redis_lock::lock($key, $redis_lock::UNBLOCK, 300);

        if (!$lock) {
            return ['code'=>201,'msg'=>'店铺发货数据并发了'];
        }*/

        if(!$data['warehouse_id'] || !$data['order_id'])  return  true;
            $log =[];
            $log['code']         = 1;
            $log['order_id']     = $data['order_id'];
            $log['shop_id']      = $data['shop_id'];
            $log['time']         = time();
            $log['warehouse_id'] = $data['warehouse_id'];
            OrderShipLogic::insertOrderShipLog($log);

            if ($data['warehouse_id'] == 3){
                $jinyiApi    = new JinyiApiService();
                $jinyiApi->createOrder($data);
            }

            if ($data['warehouse_id'] == 4){
                $shunfengApi = new ShunfengApiService();
                $shunfengApi->createOutBoundOrder($data);
            }
            try{ 
                if ($data['warehouse_id'] == 5){
                    $YiJTApi = new YiJTApiService();
                    $YiJTApi->createOrder($data);
                }
            } catch (\Exception $e) {
                htmlLog(['Exception'=>$data,'getMessage'=>$e->getMessage()],OPERATE_LOG . date('Ym') . '/createOrder/' . date('Ymd') . '.txt');
            }
       // $lock->unlock();
        return  true;
    }
}
